package com.hk.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.User;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IUserService;
import com.hk.service.impl.UserServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/8 10:58
 * @Statement: 新增会员和查询所有会员
 */
@WebServlet("/user/queryAndAdd")
public class UserQueryAndAddServlet extends HttpServlet {

    IUserService iUserService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        统一字符编码
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String gmtCreate = Tool.nullToString(req.getParameter("gmtCreate"));
        String gmtCut = Tool.nullToString(req.getParameter("gmtCut"));
        String birthday = Tool.nullToString(req.getParameter("birthday"));
        String hobby = Tool.nullToString(req.getParameter("hobby"));
        String username = Tool.nullToString(req.getParameter("username"));
        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
//        封装对象
        User user = new User();
        user.setGmtCreate(gmtCreate);
        user.setGmtModified(gmtCut);
        user.setBirthday(birthday);
        user.setHobby(hobby);
        user.setUsername(username);
//        System.out.println("User="+user);
//        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
        List<User> userList = iUserService.findAll(pageNum,size,user);
        int count = iUserService.count(-1,-1, user);
        PrintWriter out = resp.getWriter();
        if (userList.isEmpty()) {
            ResultVO<User> resultVO = new ResultVO<>(0,"error",0,userList);
            String userListJSON = JSONObject.toJSONString(resultVO);
            out.println(userListJSON);
        } else {
            ResultVO<User> resultVO = new ResultVO<>(0,"success",count,userList);
            String userListJSON = JSONObject.toJSONString(resultVO);
            out.println(userListJSON);
        }
        out.flush();
        out.close();


    }

    /**
     * 添加会员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        统一字符编码
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
//        获取前台传递的JSON数据
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        User user = gson.fromJson(reader, User.class);
//        System.out.println("获取到的值："+user);
        int save = iUserService.save(user);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (save != 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();


    }

}
